import MySQLdb
import os
import string

class _OVIMLog:
    _job_id = 0
    _component = ''
    _mysql_conn = None
    _log_cursor = None
    _printout = False
    _debug = False
    
    def __init__(self, mysql_conn, component='', job_id=0, printout=False, debug=False):
        self._job_id = job_id
        self._component = component
        self._mysql_conn = mysql_conn
        self._log_cursor = mysql_conn.cursor()
        self._printout = printout
        self._debug = debug
    
    def log(self, buffer, loglevel='INFO'):
        if (not self._debug) and (loglevel == 'DEBUG'):
            return
        
        query = "INSERT INTO vimMain_log ( id , loglevel , component , date , jobid , log )\
        VALUES (NULL , '%s', '%s', NOW( ) , '%d', '%s');" \
              % (loglevel, self._component, self._job_id,  string.replace(buffer, "'", "''"))
        try:
            self._log_cursor.execute(query)
        except Exception, err:
            if self._printout:
                print "OVIMLog::Error %s" % err
            pass
        
        if self._printout:
            print "LOG: %s" % buffer

